package com.ebelter.myehc.adapters.historys;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.EarTemperatureManager;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.response.GetTemperature;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class H_Temperature_A extends BaseAdapter {

    private Context mContext;
    private static final int[] colors = new int[]{
      0xff17d5e5,0xff14e34d,0xffffcc00,0xffff9000,0xfffc3839,0xffaa0c0b
    };


//    app:color1="#17d5e5"
//    app:color2="#14e34d"
//    app:color3="#ffcc00"
//    app:color4="#ff9000"
//    app:color5="#fc3839"
//    app:color6="#aa0c0b">

    private List<GetTemperature.ResultDataBean.DataListBean> datas;

    public H_Temperature_A(Context mContext) {
        this.mContext = mContext;
        datas=new ArrayList<>();
    }


    public void addData(List<GetTemperature.ResultDataBean.DataListBean> data){
        if (data==null||data.size()==0){
            return;
        }
        datas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas!=null?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        VH vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.item_xt_his,null);
            vh=new VH();
            vh.topLine=convertView.findViewById(R.id.item_xt_top_h_line);
            vh.buttomLine=convertView.findViewById(R.id.item_xt_buttom_h_line);
            vh.item_xt_time_tv=convertView.findViewById(R.id.item_xt_time_tv);
            vh.item_xt_date_tv=convertView.findViewById(R.id.item_xt_date_tv);
            vh.item_xt_item_tv=convertView.findViewById(R.id.item_xt_item_tv);
            vh.item_xt_item_tv.setText(BaseActivity.getString_(R.string.tiwen)+"（℃）");
            vh.item_xt_value_tv=convertView.findViewById(R.id.item_xt_value_tv);
            convertView.setTag(vh);
        }else {
            vh= (VH) convertView.getTag();
        }




        Date date = TimeUtils.parseFormatter1Time(datas.get(position).time);
        String shiFen = TimeUtils.formatTime4(date.getTime());
        //String yueRi = TimeUtils.formatTime11(date.getTime());
        String yueRi = TimeUtils.timeFatmat(datas.get(position).time,1);
        vh.item_xt_time_tv.setText(shiFen);//时-分
        vh.item_xt_date_tv.setText(yueRi);//月-日



        float temperature=datas.get(position).temperature;

        vh.item_xt_value_tv.setTextColor(colors[EarTemperatureManager.getResultLevel(temperature)]);

        vh.item_xt_value_tv.setText(temperature+"");

        adapterButtomLineView(vh.buttomLine,position,yueRi);
        return convertView;
    }


    /**
     *
     * @param buttomLine 底部横线控件
     * @param position 当前位置position
     * @param currentYueRi 当前月-日字符串
     */
    private void adapterButtomLineView(View buttomLine, int position, String currentYueRi) {

        try {
            ViewUtils.displayView(buttomLine);
            String nextYueRi = TimeUtils.timeFatmat(datas.get(position+1).time,1);
            if (TextUtils.equals(currentYueRi,nextYueRi)){
                //当前的月日日期跟下一个的月日日期是一样的 底部横线gone
                ViewUtils.goneView(buttomLine);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    static class VH{
        View topLine;
        View buttomLine;
        TextView item_xt_time_tv;
        TextView item_xt_date_tv;
        TextView item_xt_item_tv;
        TextView item_xt_value_tv;
     }
}
